#include <g-bios.h>
#include <flash/flash.h>
#include <arch/mem.h>
#include <core/cmd.h>
//fixme:
#include <flash/part.h>

extern GApp g_pGBiosAppBegin[], g_pGBiosAppEnd[];

static int help_main(int argc, char *argv[])
{
	int ret = -EINVAL;
	static int mach_name_flat = 0;
	GApp *pApp;

	switch (argc)
	{
	case 1:
		printf("\nValid commands:\n");

		for (pApp = g_pGBiosAppBegin; pApp < g_pGBiosAppEnd; pApp++)
			printf("  %-8s\t: %s\n", pApp->pchName, pApp->pchHelpMsg);

		ret = 0;
		break;

	case 2:
		for (pApp = g_pGBiosAppBegin; pApp < g_pGBiosAppEnd; pApp++)
			if (!strcmp(pApp->pchName, argv[1]))
			{
				printf("%s manual:\n\t%s\n", pApp->pchName, pApp->pchHelpMsg);
				mach_name_flat = 1;

				ret = 0;
				break;
			}

		if (!mach_name_flat)
		{
			printf("help: %s: No such command\n", argv[1]);
			ret = -EINVAL;
		}

		break;

	default:
		printf("error: arguments too much\n");

		ret = -EINVAL;
		break;
	}

	return ret;
}


int GuValToHrStr(ULONG ulVal, char str[]);

static int ls_main(int argc, char *argv[])
{
	int ret = -EINVAL;
	char szBase[32], szSize[32];
	struct Partition *pPart;
	struct PartAttr attr;


	pPart = GuPartOpenCur();
	BUG_IF(NULL == pPart);

	GuPartGetAttr(pPart, &attr);

	GuValToHrStr(attr.ulPartSize, szSize);
	GuValToHrStr(attr.ulPartBase, szBase);

	printf("\tPartition Type = \"%s\"\n"
		   "\tMTD Deivce     = /dev/mtdblock%d\n"
		   "\tPartition Base = 0x%08x (%s)\n"
		   "\tPartition Size = 0x%08x (%s)\n"
		   "\tHost Device    = %s\n",
		   GuPartTypeToStr(attr.nPartType),
		   GuPartGetIndex(pPart),
		   attr.ulPartBase, szBase,
		   attr.ulPartSize, szSize,
		   pPart->pHost->szName
		   );

	// GuValToHrStr(attr.ulImgSize, szTmp);

	printf("\tImage File     = ");

	if (attr.ulImgSize)
	{
		printf("%s (%d bytes)\n", attr.szImgName, attr.ulImgSize);
	}
	else
	{
		printf("(No image file)\n");
	}

	GuPartClose(pPart);

	return 0;
}


static int clear_main(int argc, char *argv[])
{
	printf(CLRSCREEN);

	return 0;
}


INSTALL_APPLICATION(ls, ls_main, "current dir");

INSTALL_APPLICATION(dir, ls_main, "current dir,  same as ls.");

INSTALL_APPLICATION(help, help_main,"show information for users!");

INSTALL_APPLICATION(clear, clear_main, "clear screen.");

